package name.jiaosha.air.install.controller;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.install.domain.Install;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.AliUtil;

@Controller
@RequestMapping("/admin")
public class UploadController {

	@RequestMapping("/excelUpload")
	public ModelAndView processExcelUpload(@RequestParam() MultipartFile file,
			HttpServletRequest request,HttpServletResponse response) throws Exception {
		if (!file.isEmpty()) {

			InputStream inputstream = file.getInputStream();

			// 通过得到的文件输入流inputstream创建一个HSSFWordbook对象
			HSSFWorkbook hssfworkbook;

			hssfworkbook = new HSSFWorkbook(inputstream);

			HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);// 第一个工作表

			HSSFRow hssfrow = hssfsheet.getRow(0);// 第一行抬头,不用处理

			for (int i = 0; i < 1; i++) {
				hssfsheet = hssfworkbook.getSheetAt(i);

				// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数

				for (int j = 1; j < hssfsheet.getPhysicalNumberOfRows(); j++) {

					if (hssfrow == null) {
						// System.out.println("这里已没有数据，在第" + i + "列,第" + j
						// + "行");
						// break;
					} else {
						hssfrow = hssfsheet.getRow(j);

//						System.out.println(hssfrow.getCell((short) 0)
//								.getNumericCellValue());

					}

				}

			}

			System.out.println("name: " + file.getOriginalFilename()
					+ "  size: " + file.getSize());
		}

		// return "redirect:/image"; // 跳转你所指定的页面名称
		ModelMap model = new ModelMap();
		model.addAttribute("success", true);
		//return new ModelAndView("jsonView", model);
		ObjectMapper m = new ObjectMapper();
		//m.writeValue(response.getOutputStream(), model);
		//System.out.println(response.getOutputStream().toString());
		response.setContentType("text/html;charset=UTF-8");
		//response.getWriter().write("{success:'true',root:[{'address':'杭州天堂软件园','id':1001,'phone':'1882566002','unit':'业务单位1019','buyer':'张先生','mobile':'8848520'},{'address':'杭州天堂软件园','id':1002,'phone':'1882566002','unit':'业务单位1019','buyer':'张先生','mobile':'8848520'},{'address':'杭州天堂软件园','id':1003,'phone':'1882566002','unit':'业务单位1019','buyer':'张先生','mobile':'8848520'}]}");
		
		List<Install> list = null;
		if (AppConstants.DEBUG) {
			list = new ArrayList<Install>();
			for (int i = 1; i < 10; i++) {
				Install install = new Install();
				install.setDocID(String.valueOf(i));
				install.setPartnerId(i);
				install.setBuyer("张先生");
				install.setPhone("1882566002");
				install.setMobile("8848520");
				install.setAddress("杭州天堂软件园");
				install.setRemarks("备注" + i);
				install.setCreateMan("黄光裕");
				install.setBuyDate(AliUtil.getDaysAfter(-3));
				install.setDeliverDate(AliUtil.getDaysAfter(-2));
				install.setFixDate(new Date());
				install.setInvoicesType(2);
				install.setDistrictId(1);
				list.add(install);
			}

		} else {

		}
		model.addAttribute("root", list);
		m.writeValue(response.getWriter(), model);
		
		return null;


	}

}
